<?php

namespace app\index\controller;

use think\Controller;
use app\index\model\Users;
use app\common;
use think\Db;

/**
 * Description of User
 *
 * @author Candy
 */
class User extends Controller {

    //用户注册接口
    public function regist($username, $password, $verify_code) {
        if (!checkMobile($username)) {
            return myJson(-1, '注册失败 - 手机号格式错误', '');
        }
        if (!checkVerifyCode($username, $verify_code)) {
            return myJson(-1, '验证码错误或已过期', '');
        }
        //密码长度验证
        if (strlen($password) < 6 || strlen($password) > 20) {
            return myJson(-1, '注册失败 - 密码长度为 6 - 20位', '');
        }
        //先查询该用户是否存在
        if (Users::where('user_name', $username)->count() > 0) {
            return myJson(-1, '注册失败 - 用户已存在', '');
        } else {
            try {
                $user = new Users;
                $user->user_name = $username;
                $user->user_password = md5($password . MD6);
                $user->user_nickname = "糖果TV_" . $username;
                $user->user_regist_time = date('Y-m-d H:i:s'); //日期
                $user->user_token = md5($username . $password . time());
                $user->user_token_time = time(); //--时间戳
                $user->save();
                return myJson(1, '注册成功', '');
            } catch (\Exception $e) {
                //失败
                return myJson(-1, '注册失败 - ' . $e->getMessage(), '');
            }
        }
    }

    //用户登录接口
    public function login($username, $password) {
        if (!checkMobile($username)) {
            return myJson(-1, '用户名错误', '');
        }
        //查询该用户是否存在
        $user = Users::getByUserName($username);
        if (!$user) {
            return myJson(-1, '用户不存在', '');
        }
        if (md5($password . MD6) != $user->user_password) {
            return myJson(-1, '密码错误', '');
        }
        try {
            $user->user_status = 1;
            $user->user_token = md5($username . $password . time());
            $user->user_token_time = time();
            $user->save();
            return myJson(1, '登录成功', [
                'user_token'    =>  $user->user_token,
                'user_name'     =>  $user->user_name,
                'user_nickname' =>  $user->user_nickname,
                'user_avatar'   =>  $user->user_avatar
            ]);
        } catch (\Exception $e) {
            //失败
            return myJson(-1, '登录失败 - ' . $e->getMessage(), '');
        }
    }


    //用户分页查询
    public function findUser($page_index, $page_num) {
        //如果分页的条数存在
        if ($page_num) {
            $start_index = $page_index * $page_num;

            $result = Db::name('users')->limit($start_index, $page_num)->select();
            // 逻辑处理
            //print_r($result);
            return myJson(1, '查询成功', $result);
        } else {
            $result = Db::name('users')->select();
            if ($result) {
                return myJson(1, '查询成功', $result);
            } else {
                return myJson(1, '查询成功 - 没有任何数据', '');
            }
        }
    }

    //用户信息修改
    public function resetPassword($token, $old_pass, $new_pass, $new_nickname) {
        //密码长度验证
        if (strlen($new_pass) < 6 || strlen($new_pass) > 20) {
            return myJson(-1, '密码修改失败 - 密码长度为 6 - 20位', '');
        }
        $user_id = checkToken($token);
        $user = Users::getByUserId($user_id);
        if (md5($old_pass . MD6) != $user->user_password) {
            return myJson(-1, '密码修改失败 - 原密码输入错误', '');
        }
        try {
            $user->user_password = md5($new_pass . MD6);
            $user->user_nickname = $new_nickname;
            $user->save();
            return myJson(1, '用户信息修改成功', '');
        } catch (\Exception $e) {
            return myJson(-1, '用户信息改失败 - ' . $e->getMessage(), '');
        }
    }

    //用户昵称修改
//    public function resetNickname($token, $nickname) {
//        $user_id = checkToken($token);
//        $user = Users::getByUserId($user_id);
//        try {
//            $user->user_nickname = $nickname;
//            $user->save();
//            return myJson(1, '恭喜您 - 昵称修改成功', '');
//        } catch (\Exception $e) {
//            return myJson(-1, '昵称修改失败 - ' . $e->getMessage(), '');
//        }
//    }

    //找回用户密码
    public function findPassword($username, $password, $verify_code) {
        if (!checkMobile($username)) {
            return myJson(-1, '重置密码失败 - 用户名格式错误', '');
        }
        if (!checkVerifyCode($username, $verify_code)) {
            return myJson(-1, '验证码错误', '');
        }
        //密码长度验证
        if (strlen($password) < 6 || strlen($password) > 20) {
            return myJson(-1, '注册失败 - 密码长度为 6 - 20位', '');
        }
        $user = Users::getByUserName($username);
        if (!$user) {
            return myJson(-1, '用户不存在', '');
        }
        try {
            $user->user_password = md5($password . MD6);
            $user->save();
            return myJson(1, '恭喜您 - 密码重置成功', '');
        } catch (\Exception $e) {
            return myJson(-1, '密码重置失败 - ' . $e->getMessage(), '');
        }
    }

//    //用户退出登录接口
//    public function loginOut($token) {
//        //查询该用户是否存在
//        $user = Users::get(['user_token' => $token]);
//        if ($user) {
//            try {
//                $user->user_status = 0;
//                $user->save();
//                return myJson(1, '退出登录成功', '');
//            } catch (\Exception $e) {
//                //失败
//                return myJson(-1, '退出登录失败 - ' . $e->getMessage(), '');
//            }
//        }
//    }
}
